package com.sgb.kjwl.view.widget.packers;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.YoungBaseRecycleAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * description:工队发布--工作区域
 * @author younghow
 * @date 2021/9/7
 */
public abstract class ThreeLevelForSelectAreaPopWindow<D extends AddressEntity,A extends YoungBaseRecycleAdapter<D>,B extends YoungBaseRecycleAdapter<D>,C extends YoungBaseRecycleAdapter<D>> extends BasePopWindow {

    private A mProvinceAdapter;
    private B mCityAdapter;
    private C mAreaAdapter;
    private List<D> pickSelectEntityList;
    private RecyclerView rvProvince;
    private RecyclerView rvCity;
    private RecyclerView rvArea;
    private String name;
    private WorkTypeSelectSignUpWindow.SelectResultClick<D> selectResultClick;

    public void setSelectResultClick(WorkTypeSelectSignUpWindow.SelectResultClick<D> selectResultClick) {
        this.selectResultClick = selectResultClick;
    }

    public ThreeLevelForSelectAreaPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.three_level_for_select_area;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        pickSelectEntityList = new ArrayList<>();

        TextView okView = holder.getPop().findViewById(R.id.ok_view);
        TextView cancelView = holder.getPop().findViewById(R.id.cancel_view);

        rvProvince = holder.getPop().findViewById(R.id.rv_province);
        rvCity = holder.getPop().findViewById(R.id.rv_city);
        rvArea = holder.getPop().findViewById(R.id.rv_area);

        mProvinceAdapter = createProvinceAdapter();
        mCityAdapter = createCityAdapter();
        mAreaAdapter = createAreaAdapter();

        rvProvince.setLayoutManager(new LinearLayoutManager(mContext));
        rvCity.setLayoutManager(new LinearLayoutManager(mContext));
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));

        rvProvince.setAdapter(mProvinceAdapter);
        rvCity.setAdapter(mCityAdapter);
        rvArea.setAdapter(mAreaAdapter);

        okView.setOnClickListener(v -> {
            selectResultClick.result(pickSelectEntityList, name);
            dissMiss();
        });
        cancelView.setOnClickListener(v -> dissMiss());

        if (mAreaAdapter != null){
            mAreaAdapter.setMultiSelect(true);
            mAreaAdapter.setDefItem(-1);
            rvArea.setLayoutManager(new GridLayoutManager(mContext, 4));
            rvArea.setAdapter(mAreaAdapter);
            mAreaAdapter.setOnItemMultiClick((pickSelectEntities, position) -> {
                pickSelectEntityList = pickSelectEntities;
                if (pickSelectEntities == null || pickSelectEntities.size() == 0) {
                    name = "";
                } else {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (D pickSelectEntity : pickSelectEntities) {
                        stringBuilder.append(pickSelectEntity.getName());
                        stringBuilder.append("、");
                    }
                    name = stringBuilder.substring(0, stringBuilder.length() - 1);
                }
            });
        }
    }

    private void setProvinceLayoutManager(RecyclerView.LayoutManager layoutManager){
        rvProvince.setLayoutManager(layoutManager);
        rvProvince.setAdapter(mProvinceAdapter);
    }

    private void setCityLayoutManager(RecyclerView.LayoutManager layoutManager){
        rvCity.setLayoutManager(layoutManager);
        rvCity.setAdapter(mCityAdapter);
    }

    private void setAreaLayoutManager(RecyclerView.LayoutManager layoutManager){
        rvArea.setLayoutManager(layoutManager);
        rvArea.setAdapter(mAreaAdapter);
    }

    public void setTitle(String num) {

    }

    /**
     * 设置三级的数据，不联动
     * @param provinceData
     */
    public void setThreeLevelData(List<D> provinceData,List<D> cityData,List<D> areaData) {
        mProvinceAdapter.setDatas(provinceData);
        mCityAdapter.setDatas(cityData);
        mAreaAdapter.setDatas(areaData);

        mProvinceAdapter.setDefItem(-1);
        mCityAdapter.setDefItem(-1);
        mAreaAdapter.setDefItem(-1);
    }

    public void setProvinceDefaultItem(int position){
        mProvinceAdapter.setDefItem(position);
        mProvinceAdapter.setCheckDefault(position);
    }

    public void setProvinceDefaultItems(int position){
        mProvinceAdapter.setDefItem(position);
        mProvinceAdapter.setCheckDefault(position);
    }

    public void setCityDefaultItem(int position){
        mCityAdapter.setDefItem(position);
        mCityAdapter.setCheckDefault(position);
    }

    public void setCityDefaultItems(int position){
        mCityAdapter.setDefItem(position);
        mCityAdapter.setCheckDefault(position);
    }

    public void setAreaDefaultItem(int position){
        mAreaAdapter.setDefItem(position);
        mAreaAdapter.setCheckDefault(position);
    }

    public void setAreaDefaultItems(int position){
        mAreaAdapter.setDefItem(position);
        mAreaAdapter.setCheckDefault(position);
    }

    public void setAreaListVisible(boolean visible){
        if (visible){
            rvArea.setVisibility(View.VISIBLE);
        }else {
            rvArea.setVisibility(View.GONE);
        }
    }

    protected abstract A createProvinceAdapter();

    protected abstract B createCityAdapter();

    protected abstract C createAreaAdapter();

    public interface SelectResultClick<D> {

        void result(List<D> pickSelectEntities, String name);
    }
}
